exports.InAppTableViewCell = UI.ListViewItem.subclass({
    
    className: "InAppTableViewCell",
    
    initialize: function(reuseId,cellModel,isNuggets) {
		//$super(reuseId);
    	this._isNuggets = isNuggets;
    	this._cellView = null;
    	this._indexPath_Row = 0;
        this._cellModel = cellModel;
        this._buttonEnabled = true;
        this._itemId = null;
    },
    
    _onCreateView: function() {
		this._cellView = new CustomCellView();
		
		var cellBgPath = this.getBaseResourcePath() + "cell-background.png";
		var placeholderPath = this.getBaseResourcePath() + "placeholder.png";
		var buyButtonNormalPath = this.getBaseResourcePath() + "buy-normal.png";
		var buyButtonPressedPath = this.getBaseResourcePath() + "buy-pressed.png";

        this._cellView.setFrame([TCUtil.getAdjustedX(0),TCUtil.getAdjustedY(0),TCUtil.getAdjustedX(280),TCUtil.getAdjustedY(90)]);
        
		this._cellView.backgroundView = UIFactory.getView([TCUtil.getAdjustedX(10),TCUtil.getAdjustedY(5),TCUtil.getAdjustedX(275),TCUtil.getAdjustedY(90)], null, null);
		this._cellView.addChild(this._cellView.backgroundView);
		
		this._cellView.cellBg = UIFactory.getImage([TCUtil.getAdjustedX(0), TCUtil.getAdjustedY(0), TCUtil.getAdjustedX(275),TCUtil.getAdjustedY(90)], cellBgPath);
		this._cellView.backgroundView.addChild(this._cellView.cellBg);
		
		this._cellView.placeholder = UIFactory.getImage([TCUtil.getAdjustedX(25), TCUtil.getAdjustedY(20), TCUtil.getAdjustedX(55),TCUtil.getAdjustedY(50)], placeholderPath);
		this._cellView.backgroundView.addChild(this._cellView.placeholder);

		this._cellView.buyButton = UIFactory.getGenericButton([TCUtil.getAdjustedX(180), TCUtil.getAdjustedY(50), TCUtil.getAdjustedX(70),TCUtil.getAdjustedY(30)], buyButtonNormalPath, buyButtonPressedPath, Core.Localization.getString("BUY"), "000000", TCUtil.getAdjustedFontSize(60), null, null);
		this._cellView.addChild(this._cellView.buyButton);		
		
        this._cellView._itemName = UIFactory.getTextArea([TCUtil.getAdjustedX(100), TCUtil.getAdjustedY(10), TCUtil.adjustWidthWithDevice(190), TCUtil.adjustHeightWithDevice(30)], "", TCUtil.getAdjustedFontSize(70), UI.ViewGeometry.Gravity.Left, 'ffcc99');
		this._cellView.addChild(this._cellView._itemName);
		
		this._cellView._itemDescription = UIFactory.getTextArea([TCUtil.getAdjustedX(100), TCUtil.getAdjustedY(30), TCUtil.adjustWidthWithDevice(170), TCUtil.adjustHeightWithDevice(30)], "", TCUtil.getAdjustedFontSize(60), UI.ViewGeometry.Gravity.Left, 'ffcc99');
		this._cellView.addChild(this._cellView._itemDescription);
		
		this._cellView._itemPrice = UIFactory.getTextArea([TCUtil.getAdjustedX(100), TCUtil.getAdjustedY(50), TCUtil.adjustWidthWithDevice(170), TCUtil.adjustHeightWithDevice(30)], "", TCUtil.getAdjustedFontSize(70), UI.ViewGeometry.Gravity.Left, 'ffcc99');
		this._cellView.addChild(this._cellView._itemPrice);
		
		this._cellView._thumb = UIFactory.getImage([TCUtil.getAdjustedX(30), TCUtil.getAdjustedY(22), TCUtil.getAdjustedX(45),TCUtil.getAdjustedY(45)]);
		this._cellView.backgroundView.addChild(this._cellView._thumb);
		
//		this._cellView._BigButton = UIFactory.getGenericButton([TCUtil.getAdjustedY(10),TCUtil.getAdjustedX(5),TCUtil.getAdjustedY(275),TCUtil.getAdjustedX(90)]);
//		this._cellView._BigButton.setAlpha(0.02);
//		this._cellView.addChild(this._cellView._BigButton);

        return this._cellView;
    },
    _onSetView: function(cellView) 
    {	    	
    	this._itemId = GACommonUtils.getKeyForStoreObject(this._cellModel);
    	
    	cellView._itemName.setText(this._cellModel.name);
    	cellView._itemDescription.setText(this._cellModel.description);
		
		if(this._isNuggets)
		{
			cellView._thumb.setImage("./Content/images/gamelayer/controllayer/nugget.png");
		}
		else
		{
			cellView._thumb.setImage("./Content/images/gamelayer/controllayer/coin.png");
		}

//		cellView._BigButton.onclick = function() 
//        {
//            this.onCellTap(this._cellModel);
//        }.bind(this);
//        
	    cellView.buyButton.controller = this;
		cellView.buyButton.onclick = function() 
        {
            this.onBuyTap(this._cellModel);
        }.bind(this);
    },
    
    onBuyTap: function(cellModel)
    {
    	MMSoundManager.playEffect(BUTTON_TAP_SOUND);
		UIFactory.showBlockView();
 		var user = MMGameState.getCurrentUser();
        var ip = cellModel; 

        if(this._isNuggets)
		{
            if( user.getNuggets() + ip.custom_attributes.rewardpoints >= MM_MAX_USER_NUGGETS)
            {
				var alertBox = UIFactory.getAlertBox(Core.Localization.getString("MESSAGE"), Core.Localization.getString("NUGGETS_EXCEED_LIMIT"), ["ok"], this);
                UIFactory.hideBlockView();
                return;
            }
		}
		else
		{
			if( user.getCoins() + ip.custom_attributes.rewardpoints >= MM_MAX_USER_COINS)
			{     
				var alertBox = UIFactory.getAlertBox(Core.Localization.getString("MESSAGE"), Core.Localization.getString("COINS_EXCEED_LIMIT"), ["ok"], this);
           		UIFactory.hideBlockView();
				return;
			}
		}
		
		if(this._buttonEnabled)
		{
			this._buttonEnabled = false;
			
			var inAppItemId = ip.custom_attributes.productidentifier;
	      	var callbackMethod = this.myMethodForInAppCallback.bind(this);
			GAInAppPurchaseHandler.performInAppWithItemIdAndCallback(inAppItemId, callbackMethod);
		}
	},
	
	myMethodForInAppCallback: function(errorMsg)
	{
		if(!errorMsg)
		{
			var ip = this._cellModel; 
			var user = MMGameState.getCurrentUser();
            var currencyToAward = +ip.custom_attributes.rewardpoints;
			var successMessage;
			if(this._isNuggets)
			{
                user.updateCurrency(kOperationType.kOperationAddNuggets,parseInt(currencyToAward));
                //successMessage = "You have purchased "+currencyToAward+" Nuggets successfully!";
                successMessage = GALocalizationHandler.getRelativeString(Core.Localization.getString("PURCHASED_NUGGETS_SUCCESS"),[currencyToAward]);
                AnalyticsHandler.logAnalyticsOffer('InAppPurchase',currencyToAward,'Nuggets','');
                
//                GAInAppPurchaseHandler.notifyServer(GAInAppPurchaseCurrencyType.kBucks, currencyToAward);             
//                var prevPurchasedBucks = user.getPurchasedBucks();
//                var currPurchasedBucks = +prevPurchasedBucks + currencyToAward;
//                user.setPurchasedBucks(""+currPurchasedBucks);
//                //console.log('USMAN >> InAppPurchase bucks : ' + currencyToAward);
//				  AnalyticsHandler.logAnalyticsOffer('InAppPurchase',currencyToAward,'bucks','');
			}
			else
			{
                user.updateCurrency(kOperationType.kOperationAddCoins,parseInt(currencyToAward));
                //successMessage = "You have purchased "+currencyToAward+" Coins successfully!";
                successMessage = GALocalizationHandler.getRelativeString(Core.Localization.getString("PURCHASED_COINS_SUCCESS"),[currencyToAward]);
                AnalyticsHandler.logAnalyticsOffer('InAppPurchase',currencyToAward,'Coins','');
//                
//                GAInAppPurchaseHandler.notifyServer(GAInAppPurchaseCurrencyType.kCoins, currencyToAward);             
//                var prevPurchasedCoins = user.getPurchasedCoins();
//                var currPurchasedCoins = +prevPurchasedCoins + currencyToAward;
//                user.setPurchasedCoins(""+currPurchasedCoins);
//                AnalyticsHandler.logAnalyticsOffer('InAppPurchase',currencyToAward,'coins','');
			}
			//TODO alert	
			var alertBox = UIFactory.getAlertBox(Core.Localization.getString("MESSAGE"), successMessage, ["ok"], this);		
		}
		else
		{
			if(errorMsg.showAlert)
			{
				//TODO alert errorMsg.description
				var alertBox = UIFactory.getAlertBox(Core.Localization.getString("MESSAGE"), errorMsg.description, ["ok"], this);
			}
		}
		this._buttonEnabled = true;
		UIFactory.hideBlockView();
	},
  
	getBaseResourcePath: function()
	{
		return "./Content/images/gamelayer/controllayer/storetableview/";
	},

    destroy: function() 
	{
    	
    },
});

